package day14.edu;

import java.util.concurrent.locks.ReentrantLock;

// Created: 14/09/2025, ChangJiang Ru
public class LockTest {
    public static void main(String[] args) {

    Lock l1 = new Lock();
    Lock l2 = new Lock();
    Lock l3 = new Lock();

    l1.setName("Thread 1");
    l2.setName("Thread 2");
    l3.setName("Thread 3");

    l1.start();
    l2.start();
    l3.start();
    }


}

class Lock extends Thread
{
    private static int ticket = 100;
    private static final ReentrantLock lock = new ReentrantLock();

    @Override
    public void run()
    {
        while(true)
        {
            try
            {
            lock.lock();

            if(ticket > 0)
            {
                try
                {
                    Thread.sleep(1000);
                }catch(InterruptedException e)
                {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() + " have sale, the number is : "  + ticket);
                ticket--;
            }else
                break;
            }finally
            {
                lock.unlock();

            }

        }
    }
}
